/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package efrei.ete.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Frank
 */
public class PostServlet extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //analysing request get "email" and "password"
        String title = request.getParameter("title");
        String type = request.getParameter("type");
        String detail = request.getParameter("detail");
        int owner = 11;
        Date nowTime = new Date();

        JSONObject returnMsg = new JSONObject();    //used to generating return msg
        try {
            if (this.isWriteIntoDB(owner, nowTime, title, type, detail)) {  //validating email and password
                returnMsg.append("isPass", "TRUE");
            } else {
                returnMsg.append("isPass", "FALSE");
            }
        } catch (JSONException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        response.setContentType("application/json; charset=utf-8");
        //clean the buffer
        response.setHeader("pragma", "no-cache");
        response.setHeader("cache-control", "no-cache");
        //sever recall
        PrintWriter out = response.getWriter();
        out.print(returnMsg.toString());
        out.close();
    }

    /**
     * fuction take all the information needed and write to the DB
     * @param owner
     * @param nowTime
     * @param title
     * @param type
     * @param detail
     * @return whether it is successful
     */
    private boolean isWriteIntoDB(int owner, Date nowTime, String title, String type, String detail) {
        return true;
    }
}
